import 'package:flutter/material.dart';

class AnimatedCustom extends StatefulWidget {
  const AnimatedCustom({super.key});

  @override
  _AnimatedCustomState createState() => _AnimatedCustomState();
}

class _AnimatedCustomState extends State<AnimatedCustom>
    with SingleTickerProviderStateMixin {
  late final AnimationController _animationController = AnimationController(
      vsync: this, duration: const Duration(milliseconds: 2000))
    ..repeat(reverse: true);

  late final boxHTween =
      Tween(begin: 50.0, end: 100.0).animate(_animationController);

  @override
  void dispose() {
    // TODO: implement dispose
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('自定义动画页面'),
      ),
      body: Center(
          child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          AnimatedBuilder(
            animation: _animationController,
            builder: (BuildContext context, Widget? child) {
              // child 是用来优化动画性能的，如果该动画有一些组件需要每次builder 重构，可以在外面传入
              return Opacity(
                opacity:
                    // 一种是使用  opacity:evaluate，一种是使用 Tween.animte, 然后使用tween的value
                    Tween(begin: 0.0, end: 1.0).evaluate(_animationController),
                child: Container(
                    width: 100,
                    height: boxHTween.value,
                    color: Colors.blue,
                    alignment: Alignment.center,
                    child: child),
              );
            },
            // 从外部传入之后，每次builder就不会重构该child 组件
            child: const Text(
              'hello',
              style: TextStyle(color: Colors.white, fontSize: 20),
            ),
          )
        ],
      )),
    );
  }
}
